﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathLab.Messaging;
using MathLab.Utils;

namespace MathLab.Parsing
{
    /// <summary>
    /// Class collecting all commands from input and formatting them.
    /// </summary>
    class CommandsCollector
    {
        internal List<string> createCommandsList(string inputLine)
        {
            List<string> result = new List<string>();

            if (inputLine.EndsWith(Literals.COMMAND_END))
            {
                if (inputLine.Length > 1)
                {
                    result.Add(StringUtils.TrimBlanks(inputLine.Substring(0, inputLine.Length - 1)));
                }
            }
            else
            {
                result.Add(StringUtils.TrimBlanks(inputLine));
            }

            while (!inputLine.EndsWith(Literals.COMMAND_END))
            {
                Console.Write(Literals.COMMAND_PROMPT);
                inputLine = Console.ReadLine();

                if (inputLine.EndsWith(Literals.COMMAND_END))
                {
                    if (inputLine.Length > 1)
                    {
                        result.Add(StringUtils.TrimBlanks(inputLine.Substring(0, inputLine.Length - 1)));
                    }
                }
                else
                {
                    if ("".Equals(inputLine))
                    {
                        Console.WriteLine(Messages.NO_COMMAND_END);
                    }
                    else
                    {
                        result.Add(StringUtils.TrimBlanks(inputLine));
                    }
                }
            }

            return result;
        }
    }
}
